﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace GCELibrary.Current
{
    /// <summary>
    /// 访问Session
    /// </summary>
    public class HttpSession
    {

        /// <summary>
        /// 将object插入到Session
        /// </summary>
        /// <param name="sessionKey"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetSession(string sessionKey, object value)
        {

            bool result = false;
            if (HttpContext.Current != null && HttpContext.Current.Session != null)
            {
                try
                {
                    HttpContext.Current.Session[sessionKey] = value;
                    result = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return result;
        }
        /// <summary>
        /// 获取Session中的值
        /// </summary>
        /// <param name="sessionKey">Session键值</param>
        /// <returns></returns>
        public static object GetSession(string sessionKey)
        {
            object obj = null;
            if (HttpContext.Current != null && HttpContext.Current.Session != null)
            {
                try
                {
                    obj = HttpContext.Current.Session[sessionKey];
                }
                catch (Exception exp)
                {
                    throw exp;
                }
            }

            return obj;
        }
        /// <summary>
        /// 获取Session中指定类型的值
        /// </summary>
        /// <param name="sessionKey">Session键值</param>
        /// <returns></returns>
        public static T GetSession<T>(string sessionKey)
        {
            T t = default(T);
            object obj = GetSession(sessionKey);
            if (obj != null)
            {
                if (obj is T)
                    t = (T)obj;
                else
                    throw new Exception(string.Format("对象不能强制转换为\"{0}\"类型", typeof(T).ToString()));
            }
            return t;
        }
        /// <summary>
        /// 清除Session中所有键值
        /// </summary>
        public static void ClearSession()
        {
            if (HttpContext.Current != null && HttpContext.Current.Session != null)
            {
                HttpContext.Current.Session.Clear();
            }
        }
        /// <summary>
        /// 获取当前连接的SessionId
        /// </summary>
        /// <returns></returns>
        public static string GetSessionId()
        {
            if (HttpContext.Current != null && HttpContext.Current.Session != null)
            {
                return HttpContext.Current.Session.SessionID;
            }
            else
            {
                throw new Exception("Session未实例化");
            }

        }
    }
    /// <summary>
    /// 访问Cache
    /// </summary>
    public class HttpCache
    {
        /// <summary>
        /// 申明Cache变量
        /// </summary>
        protected static System.Web.Caching.Cache _cache = null;
        /// <summary>
        /// 初始化缓存实例，将_cache绑定到HttpRuntime的缓存
        /// </summary>
        private static void InitCache()
        {
           
            try
            {
                _cache = System.Web.HttpRuntime.Cache;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 将object插入到Cache
        /// </summary>
        /// <param name="cacheKey">Cache键值</param>
        /// <param name="value">插入的值</param>
        /// <returns>true成功false失败</returns>
        public static bool SetCache(string cacheKey, object value)
        {
            if (_cache == null)
                InitCache();//初始化缓存实例
            bool result = false;
            try
            {
                //插入到缓存，System.DateTime.UtcNow为当前时间，span为时长
                _cache[cacheKey] = value;
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 将object插入到Cache
        /// </summary>
        /// <param name="cacheKey">Cache键值</param>
        /// <param name="value">插入的值</param>
        /// <param name="hours">小时</param>
        /// <returns>true成功false失败</returns>
        public static bool SetCache(string cacheKey, object value, int hours)
        {
            TimeSpan span = new TimeSpan(hours, 0, 0);//转换为时间时长，小时为单位
            return SetCache(cacheKey, value, span);
        }
        /// <summary>
        /// 将object插入到Cache
        /// </summary>
        /// <param name="cacheKey">Cache键值</param>
        /// <param name="value">插入的值</param>
        /// <param name="span">有效时长</param>
        /// <returns>true成功false失败</returns>
        public static bool SetCache(string cacheKey, object value, TimeSpan span)
        {
            if (_cache == null)
                InitCache();//初始化缓存
            bool result = false;
            try
            {
                //插入到缓存，System.DateTime.UtcNow为当前时间，span为时长
                _cache.Insert(cacheKey, value, null, System.DateTime.MaxValue, span);
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 获取Cache中的值
        /// </summary>
        /// <param name="CacheKey">Cache键值</param>
        /// <returns></returns>
        public static object GetCache(string cacheKey)
        {
            if (_cache == null)//未初始化缓存实例
            {
                return null;
            }
            else
            {
                object obj = null;
                try
                {
                    obj = _cache[cacheKey];

                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return obj;
            }
        }
        /// <summary>
        /// 获取Cache中的值
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="cacheKey">Cache键值</param>
        /// <returns></returns>
        public static T GetCache<T>(string cacheKey) where T : new()
        {
            if (_cache == null)//未初始化缓存实例
            {
                return default(T);
            }
            else
            {
                T t = default(T);
                try
                {
                    object obj = _cache[cacheKey];
                    if (obj is T)
                        t = (T)obj;
                    else
                        throw new Exception("未能将对象强制转换");
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return t;
            }
        }

        public static void Remove(string cacheKey)
        {
            if (_cache == null)
                InitCache();//初始化缓存实例
            bool result = false;
            try
            {
                //插入到缓存，System.DateTime.UtcNow为当前时间，span为时长
                _cache.Remove(cacheKey);
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }
    }
}
